<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends BaseController{
   public function edituser(){
        $uname=$_GET['uname'];
        $data['uname']=$_SESSION['uname'];
        $userModel=M('users');//表明,conf中定义前缀
        $user = $userModel->where($data)->select();

        //dump($lost);
        $this->user=$user;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }
    
    
    public function updateuser(){

            $data=$_POST;
            $file=$_FILES;
            //dump($file);
            // $data['uname']=$_SESSION['uname'];
            $uname=$data['uname'];
            //dump($id);        
            $userModel = M('users'); // 实例化User模型
            // 要修改的数据对象属性赋值
            //$userModel->create();   // 根据表单提交的POST数据创建数据对象
            //dump($data);
        //$result=$userModel->where("id=$id")->save($data);

        if ($file['photo']['size']==0) {
            //没有修改头像
            $result=$userModel->where("uname='$uname'")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Admin/photo/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                $data['photo']= $info['photo']['savename']; 
                $result=$userModel->where("uname='$uname'")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Admin/photo/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
        if($result){
             $this->success('修改成功！','../Goods/goods');
             }
            else{
                $this->error('修改失败！');
                }
    }

public function goods(){
  

       $user = M('users');
        $data['uname']=$_SESSION['uname'];
        $res = $user->where($data)->find();
        //echo $user->getDbError;
        $this->list=$res;
        //显示发布的所有二维码信息
        $goods = M('goods');//实例化news对象
        $goodsData['userid']=$data['uname'];
        $pageSize=2;
        $records=$goods->where($goodsData)->count();
       

    $link="";
     for($i=1;$i<=$totalPages;$i++) {
      $link=$link."<a href='".U("goods",array("page" => $i ))."'>第".$i."页</a>";

      # code...
     }
     $this->link=$link;
    $Page=new \Think\Page($records,$pageSize);
        $this->qrcodeList=$goods->where($goodsData)->order("id desc")->limit("$Page->firstRow,$pageSize")->select();
        trace($goodsData,'提示','user');
        //dump($goodsData);
        //echo $goods->getLastSQL;
    
        $this->link=$Page->show();


        $this->display();





            
}





//删除二维码
    public function delqrcode($id)
    {
        //$id=$_GET["id"];
        $id=I('get.id');
        $goodsmodel=M('goods');
        $res=$goodsmodel->delete($id);//==$res=$goodsmodel->where("id=$id")->delete();
        //===$data["id"]=$id;$res=$goodsmodel->where($data)->delete();
        if($res==false){E("删除失败");}else{$this->success("删除成功");}
    

    }


  public function goodsadd(){
    $goods=D("goods");
    if ($goods->create()) {
                   $newid= $goods->add();

    if (false !== $newid) {
            $qrcodeString="http://".$_SERVER['SERVER_NAME'].U("Index/detail",array("id"=>$newid));   

            $data["qrpath"]=$this->qrcode($qrcodeString);   
            $data["id"]=$newid;
            $goods->save($data);
            $this->success('二维码生成成功！');
            } else {         
        $this->error('数据错误');           
        }
    }else{
            $this->error($user->getError());
        }
  }



//生成二维码
    public function qrcode($qrcodeString){
         vendor("phpqrcode.phpqrcode");

            $model = new \QRcode();
            $path="Public/Admin/Uploads/qrcode/";
            if(!file_exists($path))
            {
                mkdir($path, 0700);
            }
      $filename=time().'.png';
            $path="Public/Admin/Uploads/qrcode/".$filename; //合成路径
            $level = 'H';
            $size = 4;
            $model::png($qrcodeString,$path, $level, $size,1);
            //框架生成二维码LOGO
            $logo = "Public/Admin/Uploads/qrcode/logo.jpg";
            $image = new \Think\Image();
            $image->open($path)->water($logo,\Think\Image::IMAGE_WATER_CENTER,100)->save($path);
            return $filename;
   
    }


//显示详细信息
    // public function detail(){
    //     $data["id"]=I('get.id');
    //     $this->goodsDetail=M("goods")->where($data)->find();
    //     $this->display();
    // }
       public function printQrcode(){
         //打印选中的二维码图片
         //取值
         $ck=$_POST["Check"];
         $in=implode(",", $ck);
         //select * from diuba_goods where id in(1,2,3);
        $this->qrcode=M('goods')->where("id in ($in)")->select();
         $this->display();
    }


public function upload(){
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Public/Admin/photo/'; // 设置附件上传根目录  //  $upload->autoSub = false;//关闭子目录，默认true
    $upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
         $info   =   $upload->upload();  // 上传文件 

    if(!$info) {       
     $this->error($upload->getError());  
      }
else{       
  //return $info;    
}                   
 }
}